<?php
/**
 * ClassName: ReservationService
 * Description:
 * @Author 周昌
 * @Create 2023/11/29 10:27
 */

namespace service\admin;


use Exception;
use libs\Tool;
use model\ArchivesModel;
use model\ReservationModel;
use service\BaseService;
use think\Db;

class ReservationService extends BaseService
{
    protected static $model='model\ReservationModel';
    public static $reservation_status=[0=>'未到期',1=>'已过期',2=>'已签到',3=>'已取消'];
    public static $reservation_status_index=['unexpire'=>0,'expire'=>1,'sign'=>2,'cancel'=>3];

    //预约状态下拉选择
    public static function reservationStatusSelect()
    {
        static $reservation_status_select=[];
        if(empty($reservation_status_select)){
            foreach (self::$reservation_status as $k=>$v){
                $row=[];
                $row['id']=$k;
                $row['name']=$v;
                $reservation_status_select[]=$row;
            }
            array_unshift($reservation_status_select,['id'=>'','name'=>'-----请选择-----']);
        }
        return $reservation_status_select;
    }
    //分配指定床位+生成wait，case +更新bed is_empty状态
    public static function allocateBed($reservation_id,$room_id,$bed_id)
    {
        $reservation_info=self::$model::getInfo(['id'=>$reservation_id]);
        $member_id=ArchivesModel::getValue(['id'=>$reservation_info['archives_id']],'member_id');
        $case_insert=[
            'archives_id'=>$reservation_info['archives_id'],
            'hospital_id'=>self::getUserHospitalId(),
            'addtime'=>time()
        ];
        $wait_insert=[
            'hospital_id'=>self::getUserHospitalId(),
            'member_id'=>$member_id,
            'archives_id'=>$reservation_info['archives_id'],
            'type'=>WaitService::$wait_type_index['treatment'],
            'num'=>WaitService::getNum(WaitService::$ward_type_index['vip'],WaitService::$is_vip_index['yes'],WaitService::$wait_type_index['treatment']),
            'name'=>$reservation_info['name'],
            'sex'=>$reservation_info['sex'],
            'status'=>WaitService::$wait_status_index['progress'],
            'room_id'=>$room_id,
            'bed_id'=>$bed_id,
            'addtime'=>time(),
            'update_time'=>time(),
            'is_vip'=>WaitService::$is_vip_index['yes'],
            'ward_type'=>WaitService::$ward_type_index['vip'],
        ];
        Db::startTrans();
        try {
            //添加case
            $case_res=CasesService::add($case_insert);
            if($case_res['code']==0){
                Db::rollback();
                return Tool::Error('case添加失败');
            }
            $wait_insert['case_id']=$case_res['data']['id'];
            $wait_res=WaitService::add($wait_insert);
            if($wait_res['code']==0){
                Db::rollback();
                return Tool::Error('wait添加失败');
            }
            //更新 床位状态
            $bed_res=BedService::edit($bed_id,['is_empty'=>BedService::$is_empty_index['no']]);
            if($bed_res['code']==0){
                Db::rollback();
                return Tool::Error('床位状态修改失败');
            }
            //更新
            $res=self::edit($reservation_id,['status'=>self::$reservation_status_index['sign'],'room_id'=>$room_id,'bed_id'=>$bed_id]);
            if($res['code']==0){
                Db::rollback();
                return Tool::Error();
            }
            Db::commit();
            return Tool::Success();

        }catch (Exception $exception){
            Db::rollback();
            return Tool::Error($exception->getMessage());
        }

    }
    //自动将过期预约更改为已过期
    public static function autoCancel()
    {
        $where=[
            ['status','=',self::$reservation_status_index['unexpire']],
            ['start_time','<',time()],
        ];
        return ReservationModel::updateInfo($where,['status'=>self::$reservation_status_index['expire']]);
    }



    //列表数据
    public static function getAjaxData($where)
    {

        $table = self::$model::order(['status'=>'asc','id'=>'asc']);
        return self::tableJsonData($table, $where, function ($data){
            $data['sex_name'] = MemberService::$sex[$data['sex']];
            $data['status_name']=self::$reservation_status[$data['status']];
            $data['status_name']=self::$reservation_status[$data['status']];
            $data['hospital_name'] = $data['hospital_id']?HospitalService::getNameByKey($data['hospital_id'],''):'';
            return $data;
        });
    }

}